package com.goodsogood.union.auth.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.ContextLoader;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

/**
 * Created by dongguochao on 10/11/2016.
 */
public class IdCardUtil {

    private static JSONObject json = new JSONObject();

    public static void main(String[] a) {
        System.out.println(JSON.toJSONString(getInfo("510213198711121613")));
    }

    public static boolean isIdCard(String idCard) {
        if (StringUtils.isEmpty(idCard) || idCard.length() != 15 || idCard.length() != 18) {
            return false;
        }
        String dateStr;
        if (idCard.length() == 18) {
            dateStr = idCard.substring(6, 14);
        }else{
            dateStr = "19"+idCard.substring(6, 12);
        }
        try {
            new SimpleDateFormat("yyyyMMdd").parseObject(dateStr);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    private final static String regex = "^[A-Za-z0-9]+$";

    public static IdCardInfo getInfo(String idCard) {
        if(StringUtils.isEmpty(idCard)||!regex.matches(idCard)){
            return null;
        }
        if (json.isEmpty()) {
            String path = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/") + File.separator;

            String fullFileName = path + "jiguan.json";

            File file = new File(fullFileName);
            Scanner scanner = null;
            StringBuilder buffer = new StringBuilder();
            try {
                scanner = new Scanner(file, "utf-8");
                while (scanner.hasNextLine()) {
                    buffer.append(scanner.nextLine());
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (scanner != null) {
                    scanner.close();
                }
            }
            json = JSON.parseObject(buffer.toString());
        }
        if (StringUtils.isEmpty(idCard)) {
            return null;
        }
//        if (idCard.length() == 18 && (!IdcardValidator.is18Idcard(idCard) || !IdcardValidator.isValidate18Idcard(idCard))) {
//            return null;
//        }
        IdCardInfo idCardInfo = new IdCardInfo();
        String b;
        String sex = idCard.length()<3?"":StringUtils.substring(idCard, -2, -1);
        switch (idCard.length()) {
            case 15:
                b = "19" + idCard.substring(6, 8) + "-" + idCard.substring(8, 10) + "-" + idCard.substring(10, 12);
//                sex = Integer.valueOf(idCard.substring(14, 15)) % 2 == 0 ? "女" : "男";
                break;
            case 18:
                b = idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
//                sex = Integer.valueOf(idCard.substring(16, 17)) % 2 == 0 ? "女" : "男";
                break;
            default:
                b = null;
        }

        try {
            sex = Integer.valueOf(sex) % 2 == 0 ? "女" : "男";
        } catch (Exception e){
            sex = "";
        }

        idCardInfo.setBirthday(b);
        idCardInfo.setSex(sex);
        idCardInfo.setNativePlace(json.getString(idCard.substring(0, 6)));
//        if (idCardInfo.getNativePlace() == null) {
//            return null;
//        }
        return idCardInfo;
    }
}
